package shuzi.util;


import shuzi.data.Database;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import com.shuzi.offer.R;

public class Dialog {

	/**
	 * 退出应用
	 * 
	 * @param context
	 * @param act
	 */
	public static void dialog(Context context, final Activity act) {
		AlertDialog.Builder bui = new Builder(context);
		bui.setMessage("是否退出？");
		bui.setTitle("温馨提示！");

		bui.setPositiveButton("是", new OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				act.finish();
				System.exit(0);

			}
		});
		bui.setNegativeButton("否", new OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {

			}
		});
		/**
		 * LayoutInflater的作用类似于 findViewById(),
		 * 不同点是LayoutInflater是用来找layout文件夹下的xml布局文件， 并且实例化！而
		 * findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。
		 */

		// 自定义Dialog中取得view中的组件widget第一种方法view plaincopy to clipboardprint?
		/*
		 * LayoutInflater lay = LayoutInflater.from(Test3Activity.this); View
		 * view = lay.inflate(R.layout.custom, null);
		 */

		// bui.setView(new EditText(context));

		// bui.create();
		bui.show();// 如果要显示对话框，一定要加上这句
	}

	public static void date_dialog(Context context, final TextView tex) {
		final DatePicker date = new DatePicker(context);
		AlertDialog.Builder bui = new Builder(context);
		bui.setTitle("請輸入出生日期！");

		bui.setPositiveButton("確定", new OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				int month = date.getMonth() + 1;
				int day = date.getDayOfMonth();
				String m = null;
				String d = null;
				if (month < 10) {
					m = "0" + String.valueOf(month);
				} else {
					m = String.valueOf(month);
				}
				if (day < 10) {
					d = "0" + String.valueOf(day);
				} else {
					d = String.valueOf(day);
				}
				/*
				 * Data.date = String.valueOf(date.getYear()+"-"+m+"-"+d);
				 * 
				 * tex.setText(Data.date);
				 */
			}

		});
		bui.setNegativeButton("取消", new OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {

			}
		});
		/**
		 * LayoutInflater的作用类似于 findViewById(),
		 * 不同点是LayoutInflater是用来找layout文件夹下的xml布局文件， 并且实例化！而
		 * findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。
		 */

		// 自定义Dialog中取得view中的组件widget第一种方法view plaincopy to clipboardprint?
		/*
		 * LayoutInflater lay = LayoutInflater.from(Test3Activity.this); View
		 * view = lay.inflate(R.layout.custom, null);
		 */

		bui.setView(date);

		bui.create();
		bui.show();// 如果要显示对话框，一定要加上这句
	}



	/**
	 * 添加成功
	 * 
	 * @param context
	 */
	public static void choiceColorDialog(final Context context) {

		AlertDialog.Builder bui = new Builder(context);

		bui.setMessage("请先选择颜色");
		bui.setPositiveButton("確定", null);
		bui.create();
		bui.show();//
	}
	
	/**
	 * 没有网络
	 * 
	 * @param context
	 */
	public static void noNetDialog(final Activity context) {

		AlertDialog.Builder bui = new Builder(context);

		bui.setMessage("没有网络，请检查网络设置");
		bui.setPositiveButton("確定", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				context.finish();

			}
		});
	
		bui.create();
		bui.show();//
	}
	
	/**
	 * @param context
	 */
	public static void netError(final Activity context) {

		AlertDialog.Builder bui = new Builder(context);

		bui.setMessage("服务器发生错误，请稍后重试");
		bui.setPositiveButton("確定", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				context.finish();

			}
		});
	
		bui.create();
		bui.show();//
	}
	
	/**
	 * 注册失败
	 * @param context
	 */
	public static void registetError(final Activity context) {

		AlertDialog.Builder bui = new Builder(context);

		bui.setMessage("注册失败，请重新注册！");
		bui.setPositiveButton("確定", null);
	
		bui.create();
		bui.show();//
	}

	/**
	 * 打电话
	 * 
	 * @param context
	 */
	public static void callDialog(final Context context) {

		AlertDialog.Builder bui = new Builder(context);

		bui.setMessage("是否拨打电话？");
		bui.setPositiveButton("確定", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
						+ Database.dealer.getDealerContactNumber()));

				context.startActivity(intent);

			}
		});
		bui.setNegativeButton("取消", null);
		bui.create();
		bui.show();//
	}
	
	/**
	 * 打电话
	 * 
	 * @param context
	 */
	public static void sentSMSDialog(final Context context) {

		AlertDialog.Builder bui = new Builder(context);

		bui.setMessage("抱歉，试用版暂不能下单!");
		bui.setPositiveButton("確定", null);
		//bui.setNegativeButton("取消", null);
		bui.create();
		bui.show();//
	}
	
	
	
	/**
	 * 短信注册
	 * 
	 * @param context
	 */
	public static void register_dialog(final Activity context,
			final String macAddress, final Handler handler) {
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(
				R.layout.phone_register, null);
		final EditText phoneNumber = (EditText) view
				.findViewById(R.id.phone_number);
		final EditText dealerContactNumber = (EditText) view
				.findViewById(R.id.deliver_number);
		final EditText deliveryAddress = (EditText) view
				.findViewById(R.id.deliver_address);

		AlertDialog.Builder bui = new Builder(context);
		// bui.setTitle("温馨提示！");
		
		bui.setView(view);
		bui.setPositiveButton("注册", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				new Thread(new Runnable() {

					@Override
					public void run() {
						final String data = Webserice.saveTermInfo(macAddress,
								phoneNumber.getText().toString(),
								dealerContactNumber.getText().toString(),
								deliveryAddress.getText().toString());
						context.runOnUiThread(new Runnable() {

							@Override
							public void run() {
								System.out.println(data
										+ Webserice.isRegisterSuccess(data));
								// Toast.makeText(context,
								// data+Webserice.isRegisterSuccess(data),
								// Toast.LENGTH_LONG).show();
								if (Webserice.isRegisterSuccess(data)) {
									
									
									MySharedPreferences
											.writeStringSharedPreferences(
													context,
													Database.LOGIN_FILE_NAME,
													Database.PHONE_NUMBER, phoneNumber.getText().toString());
									MySharedPreferences
									.writeStringSharedPreferences(
											context,
											Database.LOGIN_FILE_NAME,
											Database.DELIVER_NUMBER, dealerContactNumber.getText().toString());
									MySharedPreferences
									.writeStringSharedPreferences(
											context,
											Database.LOGIN_FILE_NAME,
											Database.DELIVER_ADDRESS, deliveryAddress.getText().toString());
									handler.sendEmptyMessage(10);
								} else {
									handler.sendEmptyMessage(Database.HANDLER_REFRESH_ERROR);
								}
							}
						});

					}
				}).start();

			}
		});
		bui.setNegativeButton("取消", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				context.finish();
				System.exit(0);

			}
		});
		bui.create();
		bui.show();// 如果要显示对话框，一定要加上这句
	}

}
